﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/12/16 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Diagnostics;
using System.Drawing;

namespace BusinessLibrary
{
    /// <summary>
    /// HIOKIST5520类
    /// </summary>
    public class HIOKIST5520 : IPlugin
    {
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "HIOKIST5520相关业务处理";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null
                && APPInstance.PLC != null
                && APPInstance.PLC.IsConnected)
            {
                string identity = pluginParam.Identity.ToUpper();
                switch (identity)
                {
                    case "HIOKIST5520_RESULT_PLC": result = ResultPLC(pluginParam); break;
                    case "HIOKIST5520_STATUS_PLC": result = HIOKIStatus(pluginParam); break;
                }
            }
            return result;
        }
        /// <summary>
        /// HIOKIST5520_STATUS_PLC 状态
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string HIOKIStatus(PluginParamEntity pluginParam)
        {
            string methodName = "HIOKIST5520状态";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var statusEntity = CommonHelper.GetJson(pluginParam.ParamString, new HIOKIStatusExcuteEntity());
                try
                {
                    if (statusEntity != null && !string.IsNullOrWhiteSpace(statusEntity.StatusAddress))
                    {
                        this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 地址：{statusEntity.StatusAddress}, 状态：{statusEntity.IsOK}");
                        APPInstance.PLC.Write(statusEntity.StatusAddress, statusEntity.IsOK ? (short)1 : (short)0);
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return string.Empty;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// HIOKIST5520_RESULT_PLC
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string ResultPLC(PluginParamEntity pluginParam)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            int writerType = 6;
            string methodName = "5520结果写入PLC";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            try
            {
                if (APPInstance.PLC != null
                    && APPInstance.PLC.IsConnected
                    && !string.IsNullOrWhiteSpace(pluginParam.ParamString))
                {
                    var st5520 = CommonHelper.GetJson(pluginParam.ParamString, new HIOKIST5520ExcuteEntity()
                    {
                        JudgeValueResult = 0,
                        JudgeResult = "OFF",
                        MeasureResult = 0,
                    });
                    //判定值如果需要转换可以在这里搞
                    if (st5520 != null && st5520.JudgeValueResult == 1)
                    {
                        st5520.JudgeValueResult = (short)ENGCode.OK;
                    }
                    else
                    {
                        st5520.JudgeValueResult = (short)ENGCode.NG_INSULATION;
                    }

                    this.Info($"【{pluginParam.Tag}】{methodName} - 写PLC, 结果：{CommonHelper.GetJsonString(st5520, Newtonsoft.Json.Formatting.None)}");


                    if (!string.IsNullOrWhiteSpace(pluginParam.Tag))
                    {
                        var temp = pluginParam.Tag.Replace("ST5520-", "");
                        int tempValue = 1;
                        if (!string.IsNullOrWhiteSpace(temp) && int.TryParse(temp, out tempValue))
                        {
                            writerType = tempValue + 5;
                        }
                    }
                    writerType = writerType <= 6 || writerType >= 11 ? 6 : writerType;

                    APPInstance.PLC.Write(st5520.PlcEntity.ResultAddress, st5520.MeasureResult, true, writerType);
                    APPInstance.PLC.Write(st5520.PlcEntity.JudgeResultAddress, st5520.JudgeValueResult, true, writerType);
                }
            }
            catch (Exception ex)
            {
                this.Error(ex);
            }
            finally
            {
                stopwatch.Stop();
                this.Info($"【{pluginParam.Tag}】【统计】{methodName}，Writer：{writerType}，耗时：{stopwatch.ElapsedMilliseconds} 毫秒");
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return string.Empty;
        }
    }
}
